<template>
	<view class="container">
		<view class="cart-container" v-if="cart.length !== 0">
			<!-- 顶部收货地址 -->
			<cus-address></cus-address>
			<!-- 购物车商品列表标题-->
			<view class="cart-title">
				<uni-icons type="shop" size="20"></uni-icons>
				<text class="cart-title-text">购物车</text>
			</view>
		
			<!-- 商品列表区域 -->
			<uni-swipe-action>
				<block v-for="(item, index) in cart" :key="index">
					<uni-swipe-action-item :right-options="options" @click="swiperAction(item)">
						<cus-good :goods="item" :showRadio="true" :showNum="true" @changeRadio="changeRadio"
							@changeNum="changeNumHandler"></cus-good>
					</uni-swipe-action-item>
				</block>
			</uni-swipe-action>
			
			<!-- 商品结算 -->
			<cus-settle></cus-settle>
			
		</view>
		
		<!-- 空白购物车页面 -->
		<view class="empty-cart" v-else @click="toHome">
			<image src="/static/cart_empty@2x.png" class="empty-img"></image>
			<text class="empty-text">您的空空如也，快去购物吧！！！</text>
		</view>
	</view>
</template>

<script>
	import mixBadge from '../../mixins/tabbar-badge.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		mixins: [mixBadge],
		computed: {
			...mapState('m_cart', ['cart'])
		},
		data() {
			return {
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#c00000'
					}
				}]
			}
		},

		methods: {
			...mapMutations('m_cart', ['updateRadio', 'updateNum', 'removeCart']),
			// 修改勾选状态
			changeRadio(e) {
				this.updateRadio(e)
				console.log(e)
			},

			// 修改商品数量
			changeNumHandler(e) {
				// console.log(e);
				this.updateNum(e)
				console.log(e);
			},

			// 滑动删除
			swiperAction(goods) {
				// console.log(goods);
				this.removeCart(goods)
			},
			
			// 跳转到首页
			toHome() {
				uni.switchTab({
					url:'/pages/home/home'
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding-bottom: 50px;
	}
	.cart-title {
		height: 40px;
		font-size: 14px;
		display: flex;
		align-items: center;
		border: 1px solid #efefef;
		padding-left: 5px;

		.cart-title-text {
			margin-left: 10px;
		}
	}
	.empty-cart {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 150px;
		.empty-img {
			width: 90px;
			height: 90px;
		}
		.empty-text {
			font-size: 12px;
			margin-top: 10px;
			color: gray;
		}
	}
</style>
